function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return (r[2]);
    return null; //返回参数值
}

var orderNum = getUrlParam("orderNum");
var gateway = getUrlParam("gateway");
var payerId = getUrlParam("payerId");
var type = "N";
var bindId = null;
var accessBindId = null;
var orderIp = null;
var phone = null;
var token = null;
var time = 0;
var timer = null;
var payType = 'alipay';
var amount = "0";


$(function () {
    payType = getUrlParam("type");
    amount = getUrlParam("amount");
    type = getUrlParam("type");
    document.getElementById("select-card").style.display = "block";
    $.getJSON("/thmlfApi/avs/pay/getpayerallbind?gateway=ybpay" + "&payerId=" + payerId + "&_d=" + Date.parse(new Date()), null, function (r) {
        if (r.code == 200) {
            $.each(r.result, function (index, data) {
                var cardType = data.cardType == "X" ? "信用卡" : "借记卡";
                var item = '<div class="mui-input-row mui-radio mui-left bind-card-item" data-token="' + data.token + '" data-access-bind-id="' + data.accessBindId + '" data-bindid="' + data.bindId + '" data-phone="' + data.phone + '">' +
                    '<label>' + data.cardName + '&nbsp;&nbsp;' + cardType + '&nbsp;&nbsp;' + data.holdName + '&nbsp;&nbsp;' + '尾号' + data.lastFourNum + '</label>' +
                    '<input name="card" type="radio">' +
                    '</div>';
                $(".add-new-card").before(item);
            });
            bindChoose();
        }
    });
    $("#addBindCard").click(function () {
        bindId = null;
        window.location.href = "card-manage.html";
    });
});

// 确认信息
mui(document.body).on('tap', '.confirm-btn', function () {
    var that = mui(this);
    if (accessBindId == null && payType != 'wechat' && payType != 'alipay') {
        mui.toast("请选择银行卡");
        return;
    }
    that.button('loading');
    $.ajax({
        url: "/thmlfApi/avs/ybpay/createPay",
        data: {
            "payerId": payerId,
            "cashBindId": accessBindId,
            "orderNum": orderNum,
            "type":type
        },
        dataType: "JSON",
        type: "POST",
        success: function (r) {
            that.button('reset');
            if (r.code == 200) {
                console.log("result", r);
                window.location.href = r.result;
            } else {
                mui.alert(r.message);
            }
        },
        error: function (e) {
            that.button('reset');
            console.error("request error", e);
            mui.toast("服务器繁忙");
        }
    })
})
;


// 验证码计时器
function times() {
    if (time > 0) {
        $("#sms_code_btn").text(time + 's');
        time--;
    } else {
        clearInterval(timer);
        time = 0;
        $("#sms_code_btn").text('获取');
    }
}

// 选择银行卡
function bindChoose() {
    $(".bind-card-item").click(function () {
        bindId = $(this).attr("data-bindid");
        phone = $(this).attr("data-phone");
        token = $(this).attr("data-token");
        accessBindId = $(this).attr("data-access-bind-id");
    })
}